Ext.define('FirstMate.controller.Settings',
				{
				extend : 'Ext.app.Controller',
			    store: null,
				views : [ 'Settings' ],
				config : {
					refs : {
//						radarOnField : '#radarOnField',
						radarTimestampField : '#radarTimestampField',	
						radarResolutionField : '#radarResolutionField',						
						weatherField : '#weatherField',
						cloudField : '#cloudField',
						KmlField : '#KmlField',
						bicycleField : '#bicycleField',
						radiusField : '#radiusField',
						KmlUrlField : '#KmlUrlField',
						useCurrentTimeField : '#useCurrentTimeField',
						asOfDateField : '#asOfDateField',
						searchUnitsField : '#searchUnitsField',
						tideUnitsField : '#tideUnitsField',
						kmlUrlField : '#KmlUrlField'
					}			
				},
				init : function () {
					this.store = Ext.create('FirstMate.store.Settings');
					this.loadSettings();
				},
								
				launch : function() {
				
//					this.getRadarOnField().setValue(FirstMate.app.HRRadarLayerOn);
//					this.getRadarOnField().on ({
//						 change : function (widget) {
//	                    	   FirstMate.app.HRRadarLayerOn = widget.getValue();
//	                    	   FirstMate.app.getController('Map').addHRRadarLayer();
//		                 	   FirstMate.app.getController('Settings').saveSettings();
//	                       }
//					});

					this.getRadarResolutionField().setValue(FirstMate.app.HRRadarLayerResolution);
					this.getRadarResolutionField().setDisabled(FirstMate.app.paidVersion? false : true);
					this.getRadarResolutionField().on ({
						 change : function (widget) {
	                    	   FirstMate.app.HRRadarLayerResolution = widget.getValue();
	                    	   FirstMate.app.getController('Map').addHRRadarLayer();
		                 	   FirstMate.app.getController('Settings').saveSettings();
	                       }
					});

					
					this.getKmlUrlField().setValue(FirstMate.app.KmlLayerUrl);
					this.getKmlUrlField().on ({
						 change : function (widget) {
	                    	   FirstMate.app.KmlLayerUrl = widget.getValue();
	                    	   FirstMate.app.KmlLayerOpacity = 0.5;
	                    	   FirstMate.app.getController('Map').addKmlLayer();
		                 	   FirstMate.app.getController('Settings').saveSettings();
	                       }
					});

					this.getUseCurrentTimeField().setValue(FirstMate.app.useCurrentTime);
					this.getUseCurrentTimeField().on ({
						 change : function (widget) {							
							 FirstMate.app.useCurrentTime = widget.getValue();
							 if (FirstMate.app.useCurrentTime == 1){
								 FirstMate.app.getController('Settings').getAsOfDateField().setDisabled(true);
								 
							 }else{
								 FirstMate.app.getController('Settings').getAsOfDateField().setDisabled(false);
							 }
							 FirstMate.app.getController('Settings').setAppDate();
							 FirstMate.app.getController('Settings').getAsOfDateField().setValue(FirstMate.app.date);
							
							 //Ext.getStore('tidesStore').loadAll(FirstMate.app.date);
							 FirstMate.app.getController('Settings').saveSettings();
	                       }
					});
					this.getAsOfDateField().setValue(FirstMate.app.date);
					this.getAsOfDateField().setDisabled(FirstMate.app.useCurrentTime? true : false);
					this.getAsOfDateField().on ({
						 change : function (widget,value) {							
							 if (FirstMate.app.date == null){
								 
								 
							 }else{
								 FirstMate.app.date = value;
								 FirstMate.app.getController('Settings').setAppDate();
									console.log('DATE on CHANGE ');
									 Ext.getStore('tidesStore').loadAll(FirstMate.app.date); 
								 
			                 	 FirstMate.app.getController('Settings').saveSettings();
								 
							 }

		                 	 }
					});
					
					this.getTideUnitsField().setValue(FirstMate.app.tideUnits);
					this.getTideUnitsField().on ({
						 change : function (widget,value) {							
							 if (FirstMate.app.openTidesInfoWindow) {
								 FirstMate.app.openTidesInfoWindow.close();
							 };
							 FirstMate.app.tideUnits = value;
							 Ext.getStore('marineWeatherStore').load();
							 FirstMate.app.getController('Map').addWeatherLayer();
							 FirstMate.app.getController('Settings').saveSettings();
	                       }
					});
					
					this.getSearchUnitsField().setValue(FirstMate.app.searchUnits);
					this.getSearchUnitsField().on ({
					       change : function (widget,value) {
				    			FirstMate.app.searchUnits = value;
				    			FirstMate.app.bounds = FirstMate.app.getController('Map').getBounds(FirstMate.app.Marker.getPosition());
				    			FirstMate.app.getController('Map').removeTidesMarkers();
								Ext.getStore('tidesStationsStore').filterByGeoBounds(FirstMate.app.bounds);
								FirstMate.app.getController('Settings').setAppDate();
								FirstMate.app.getController('Settings').getAsOfDateField().setValue(FirstMate.app.date);
								//Ext.getStore('tidesStore').loadAll(FirstMate.app.date);
		                 	    FirstMate.app.getController('Settings').saveSettings();
				       }
					});	

			    	   					
					this.getWeatherField().setValue(FirstMate.app.weatherLayerOn);
					this.getWeatherField().on ({
						 change : function (widget) {
	                    	   FirstMate.app.weatherLayerOn = widget.getValue();
	                    	   FirstMate.app.getController('Map').addWeatherLayer();
		                 	   FirstMate.app.getController('Settings').saveSettings();
	                       }
					});	
					
					this.getKmlField().setValue(FirstMate.app.KmlLayerOn);
					this.getKmlField().on ({
						 change : function (widget) {
	                    	   FirstMate.app.KmlLayerOn = widget.getValue();
	                    	   FirstMate.app.getController('Map').addKmlLayer();
		                 	   FirstMate.app.getController('Settings').saveSettings();
	                       }
					});
					
					this.getCloudField().setValue(FirstMate.app.cloudLayerOn);
					this.getCloudField().on ({
						 change : function (widget) {
	                    	   FirstMate.app.cloudLayerOn = widget.getValue();
	                    	   FirstMate.app.getController('Map').addCloudLayer();
		                 	   FirstMate.app.getController('Settings').saveSettings();
	                       }
					});
					
					this.getBicycleField().setValue(FirstMate.app.bicycleLayerOn);
					this.getBicycleField().on ({
						 change : function (widget) {
	                    	   FirstMate.app.bicycleLayerOn = widget.getValue();
	                    	   FirstMate.app.getController('Map').addBicycleLayer();
		                 	   FirstMate.app.getController('Settings').saveSettings();
	                       }
					});
					
					this.getRadiusField().setValue(FirstMate.app.searchRadius);
					this.getRadiusField().on ({
						 change : function (widget) {
                 	   		FirstMate.app.searchRadius = widget.getValue();
                	   		FirstMate.app.bounds = FirstMate.app.getController('Map').getBounds(FirstMate.app.Marker.getPosition());
                	   		FirstMate.app.getController('Map').removeTidesMarkers();
        	    			Ext.getStore('tidesStationsStore').filterByGeoBounds(FirstMate.app.bounds);
        					FirstMate.app.getController('Settings').setAppDate();
        					FirstMate.app.getController('Settings').getAsOfDateField().setValue(FirstMate.app.date);
        	    			//Ext.getStore('tidesStore').loadAll(FirstMate.app.date);
	                 	    FirstMate.app.getController('Settings').saveSettings();
	                       }
					});
															
//			        //if (!FirstMate.app.Marker){
//					FirstMate.app.Marker = new google.maps.Marker({
//						position: FirstMate.app.position,
//						map: Ext.getCmp('extmap').getMap(),
//						icon : 'app/resources/icons/sailing.png',
//						visible: true,
//						title : 'Drag me around to change the search area',
//						draggable : true,
//						raiseOnDrag : true
//					});
//					google.maps.event.addListener(FirstMate.app.Marker, 'dragend', function() { 
//						console.log('DRAGEND');
//						FirstMate.app.getController('Map').removeTidesMarkers();
//						FirstMate.app.position = FirstMate.app.Marker.getPosition();
//						FirstMate.app.Marker.setTitle(FirstMate.app.Marker.getPosition().toString());
//						Ext.getCmp('extmap').getMap().setCenter(FirstMate.app.Marker.getPosition());
//						FirstMate.app.bounds = FirstMate.app.getController('Map').getBounds(FirstMate.app.Marker.getPosition());
//						Ext.getStore('tidesStationsStore').filterByGeoBounds(FirstMate.app.bounds);
//						FirstMate.app.getController('Settings').setAppDate();
//						 FirstMate.app.getController('Settings').getAsOfDateField().setValue(FirstMate.app.date);
//						//Ext.getStore('tidesStore').loadAll(FirstMate.app.date);
//						Ext.getStore('marineWeatherStore').loadWeather(FirstMate.app.position);
//						FirstMate.app.getController('Map').addNOAAZonesLayer();
//						FirstMate.app.getController('Map').addHRRadarLayer();
//					});
			        //}
					        	   		
        	   		//FirstMate.app.getController('Map').resetMap();
					Ext.getCmp('extmap').getMap().setZoom(FirstMate.app.zoomLevel);
        	 
					FirstMate.app.getController('Map').addKmlLayer();
             	   //FirstMate.app.getController('Map').addNOAAZonesLayer();
					FirstMate.app.getController('Map').addBicycleLayer();
					FirstMate.app.getController('Map').addCloudLayer();
					FirstMate.app.getController('Map').addWeatherLayer();
					
					
				},
				loadSettings : function (){
					try
					  {
						this.store.removeAll();
						this.store.load();
						
//						FirstMate.app.HRRadarLayerOn = this.__loadSetting('radarLayerOn', FirstMate.app.HRRadarLayerOn);
						FirstMate.app.HRRadarLayerResolution = this.__loadSetting('radarLayerResolution', FirstMate.app.HRRadarLayerResolution);
						FirstMate.app.zoomLevel = this.__loadSetting('zoomLevel', FirstMate.app.zoomLevel);
						FirstMate.app.tideUnits = this.__loadSetting('tideUnits', FirstMate.app.tideUnits);
						FirstMate.app.searchUnits = this.__loadSetting('searchUnits', FirstMate.app.searchUnits);
						FirstMate.app.searchRadius = this.__loadSetting('searchRadius', FirstMate.app.searchRadius);

						FirstMate.app.KmlLayerOpacity = this.__loadSetting('KmlLayerOpacity', FirstMate.app.KmlLayerOpacity);
						FirstMate.app.bicycleLayerOn = this.__loadSetting('bicycleLayerOn', FirstMate.app.bicycleLayerOn);
						FirstMate.app.weatherLayerOn = this.__loadSetting('weatherLayerOn', FirstMate.app.weatherLayerOn);
						
						FirstMate.app.KmlLayerOn = this.__loadSetting('KmlLayerOn', FirstMate.app.KmlLayerOn);
						FirstMate.app.KmlLayerUrl = this.__loadSetting('KmlLayerUrl', FirstMate.app.KmlLayerUrl);
						FirstMate.app.cloudLayerOn = this.__loadSetting('cloudLayerOn', FirstMate.app.cloudLayerOn);
						//FirstMate.app.useCurrentTime = this.__loadSetting('useCurrentTime', FirstMate.app.useCurrentTime);
						this.fireEvent('settingsLoaded');
					  }
					catch(err)
					  {
						Ext.Msg.alert('Problem Loading Settings.', 'Please use the Application Manager to clear the app data', Ext.emptyFn);
						//this.fireEvent('settingsLoaded');
					  };
   					
				},
				
				saveSettings : function (){
				    
					try
					  {
						this.store.removeAll();
						this.__saveSetting('zoom', FirstMate.app.zoomLevel);
						this.__saveSetting('radarLayerResolution', FirstMate.app.HRRadarLayerResolution);
//						FirstMate.app.HRRadarLayerOn = this.__loadSetting('radarLayerOn', FirstMate.app.HRRadarLayerOn);
						this.__saveSetting('tideUnits', FirstMate.app.tideUnits);
						this.__saveSetting('searchUnits', FirstMate.app.searchUnits);
						this.__saveSetting('searchRadius', FirstMate.app.searchRadius);
						this.__saveSetting('KmlLayerOpacity', FirstMate.app.KmlLayerOpacity);
						this.__saveSetting('bicycleLayerOn', FirstMate.app.bicycleLayerOn);
						this.__saveSetting('weatherLayerOn', FirstMate.app.weatherLayerOn);
						this.__saveSetting('KmlLayerUrl', FirstMate.app.KmlLayerUrl);
						this.__saveSetting('KmlLayerOn', FirstMate.app.KmlLayerOn);
						this.__saveSetting('cloudLayerOn', FirstMate.app.cloudLayerOn);
						//this.__saveSetting('useCurrentTime', FirstMate.app.useCurrentTime);
					    
						this.store.sync();
						this.fireEvent('settingsSaved');
						
					  }
					catch(err)
					  {
					  
						Ext.Msg.alert('Problem Saving Settings.', 'Please use the Application Manager to clear the app data', Ext.emptyFn);
						//this.fireEvent('settingsSaved');
						
					  };
					
				},
				
				__loadSetting : function (name,value){
					
					var i = this.store.findExact('key',name);
			        if(i!==-1)
			        {
			        	value = this.store.getAt(i).get('value');
			        }
			        return value;
				},
				
				__saveSetting : function (name,value){				
					this.store.add({key:name,value:value});
			        return value;
				},
				
				setAppDate : function (){
					
					FirstMate.app.date = FirstMate.app.useCurrentTime == 1 ? new Date() : FirstMate.app.date;
					//FirstMate.app.getController('Settings').getAsOfDateField().setValue(FirstMate.app.date);
					//console.log(FirstMate.app.date);
					
				}

});
